package com.yspa.knowledeUser.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.hsnn.medstgmini.dao.HibernateSqlUtil;
import com.hsnn.medstgmini.util.Pagination;
import com.yspa.knowledeUser.dao.KnowledeUserDao;
import com.yspa.knowledeUser.model.form.KnowledeUserForm;

@Repository
public class KnowledeUserDaoImpl implements KnowledeUserDao {

	@Autowired
	private HibernateSqlUtil dao;
	public Pagination getKnowledeUserList(Pagination page){
		StringBuffer sql = new StringBuffer();
		sql.append("select * from knowlede_user")
		.append(" where 1=1 ");
		Map<String, Object> params = new HashMap<String, Object>();
		if (StringUtils.isNotBlank(page.getConditions().get("username"))){
			sql.append(" and knowlede_name like :knowledeName");
			params.put("knowledeName", "%" + page.getConditions().get("username").trim() + "%");
		}
		if (StringUtils.isNotBlank(page.getConditions().get("title"))){
			sql.append(" and title like :title");
			params.put("title", "%" + page.getConditions().get("title").trim() + "%");
		}
		return dao.getSqlPageMapList(sql.toString(), params, page);
	}
	public void addUser(KnowledeUserForm form){
		StringBuffer sql = new StringBuffer();
		sql.append("insert into knowlede_user(id,knowlede_name,title,content,img,create_time,is_top,status)")
		.append(" value(:id,:knowledeName,:title,:content,:img,sysdate(),:isTop,:status)");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", UUID.randomUUID().toString());
		params.put("knowledeName", form.getKnowledeName());
		params.put("title", form.getTitle());
		params.put("content", form.getContent());
		params.put("img", form.getImgPath());
		params.put("isTop", form.getIsTop());
		params.put("status", 1);
		dao.executeSql(sql.toString(), params);
	}
	public List getEditInfo(String id){
		String sql = "select * from knowlede_user where id=:id";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		return dao.getSqlMapList(sql, params);
	}
	public void editInfo(KnowledeUserForm form){
		StringBuffer sql = new StringBuffer();
		sql.append("update knowlede_user")
		.append(" set knowlede_name=:knowledeName,")
		.append("title=:title,")
		.append("content=:content,")
		.append("img=:img,")
		.append("is_top=:isTop")
		.append(" where id=:id");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", form.getId());
		params.put("knowledeName", form.getKnowledeName());
		params.put("title", form.getTitle());
		params.put("content", form.getContent());
		params.put("img", form.getImgPath());
		params.put("isTop", form.getIsTop());
		dao.executeSql(sql.toString(), params);
	}
	public void deleteUser(List<String> list){
		String sql = "delete from knowlede_user where id in (:ids)";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("ids", list);
		dao.executeSql(sql, params);
	}
}
